/*
* @author : TF-BJ-C064
* @creation : 2014-8-11 下午9:04:00
* @description : 
*
*/

package com.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;

import com.base.BaseDao;
import com.bean.HotAreaBean;
import com.bean.Location;

public class HotAreaDao extends BaseDao{
	
	private Logger log = Logger.getLogger(HotAreaDao.class);
	
	public List<HotAreaBean> query(Integer cityid) throws Exception{
		session = this.getSession();
		String hql = "select new com.bean.HotAreaBean(po.parkinfo.longitude, po.parkinfo.latitude, count(po)) " +
				" from ParkOrder po, Parkinfo pi where pi.PId=po.parkinfo.PId and po.status=20  ";
		if(cityid!=null && cityid>0){
			String cityIds = cityid + " ";
			List<Location> listLocation =  session.createQuery("from Location where pid="+cityid ).list();
			if(listLocation!=null){
				for(Location loc : listLocation){
					cityIds += ","+ loc.getId();
				}
			}
			hql += " and pi.location in ("+cityIds+")";
		}
		hql += " group by pi.PId ";
		log.info( hql );
		Query query = session.createQuery(hql);
		List list = query.list();
		if(list!=null)
			log.info("list.size="+list.size());
		return list;
	}
	
	public List<Location> getCityList(Integer pid) throws Exception {
		session = this.getSession();
		String hql = "from Location ";
		if(pid!=null && pid>0)
			hql += " where pid="+pid;
		Query query = session.createQuery(hql);
		List<Location> list1 = query.list();
		List<Location> list = new ArrayList<Location>();
		if(pid!=null && pid>0)
			return list1;
		if(list1!=null){
			//set root location
			for(Location loc : list1){
				if(loc.getPid()==0)
					list.add(loc);
			}
			//set sub location
			for(Location loc : list1){
				if(loc.getPid()!=0){
					for(Location ploc : list){
						if(ploc.getId()==loc.getPid()){
							ploc.add(loc);
							break;
						}
					}
				}
			}
		}
		
		
		return list;
	}

}
